package com.collect.senegal.zhiScene.zhiWidget.zhiDialog;

import android.annotation.SuppressLint;
import android.app.Dialog;
import android.content.Context;
import android.os.Bundle;
import android.text.Editable;
import android.text.TextUtils;
import android.text.TextWatcher;
import android.util.Log;
import android.widget.EditText;
import android.widget.TextView;

import com.collect.senegal.R;
import com.collect.senegal.zhiUtils.StringUtil;
import com.collect.senegal.zhiUtils.ToastUtil;

import java.math.BigDecimal;

public class ZHIDialogChangeAmount extends Dialog implements TextWatcher {

    private TextView tv_title;
    private EditText et_change_amount;
    private TextView btn_negtive;


    private String mOriAmount;
    private String mDefaultAmount;
    private String mChanageAmount;

    private CTDialogChangeAmountListener mCTDialogChangeAmountListener;

    private Context mContext;

    public ZHIDialogChangeAmount(Context context) {
        super(context, R.style.CustomDialog);
        this.mContext = context;
    }

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.dialog_change_amount);
        initView();
        initViewEvent();
    }

    @Override
    public void show() {
        super.show();
        refreshViewData();
    }

    @Override
    public void beforeTextChanged(CharSequence s, int start, int count, int after) {

    }

    @Override
    public void onTextChanged(CharSequence s, int start, int before, int count) {

    }

    @Override
    public void afterTextChanged(Editable s) {

    }

    /**
     * 初始化界面控件
     *
     * @type method
     * @author liwenjie
     * @creatTime 2019-10-24 13:47
     */
    private void initView() {
        tv_title = (TextView) findViewById(R.id.tv_title);
        et_change_amount = (EditText) findViewById(R.id.et_change_amount);
        btn_negtive = (TextView) findViewById(R.id.btn_negtive);
        et_change_amount.addTextChangedListener(this);

        et_change_amount.addTextChangedListener(new TextWatcher() {
            @Override
            public void beforeTextChanged(CharSequence s, int start, int count, int after) {

            }

            @Override
            public void onTextChanged(CharSequence s, int start, int before, int count) {

            }

            @Override
            public void afterTextChanged(Editable s) {
                if (et_change_amount.getText().toString().trim().startsWith("0")) {
                    et_change_amount.setText("");
                }
                if (et_change_amount.getText().toString().trim().startsWith(".")) {
                    et_change_amount.setText("");
                }
            }
        });
    }

    @SuppressLint("SetTextI18n")
    private void refreshViewData() {
        tv_title.setText(mContext.getResources().getString(R.string.default_repay_amount) + StringUtil.safeString(mDefaultAmount));
        String showAmount = StringUtil.safeString(mChanageAmount);
        et_change_amount.setText(showAmount);
        et_change_amount.setSelection(showAmount.length());
    }

    private void initViewEvent() {
        btn_negtive.setOnClickListener(v -> {
            if (mCTDialogChangeAmountListener != null) {
                mChanageAmount = StringUtil.safeString(et_change_amount.getText().toString().trim());
                if (!TextUtils.isEmpty(mChanageAmount) && !TextUtils.isEmpty(mOriAmount)) {
                    //判断是否大于本金
//                    BigDecimal oriBig = new BigDecimal(mOriAmount);
                    BigDecimal oriBig = new BigDecimal(0);
                    BigDecimal defaultBig = new BigDecimal(mDefaultAmount);
                    BigDecimal changeBig = new BigDecimal(mChanageAmount);

                    int result0 = changeBig.compareTo(defaultBig);
                    if (result0 == 1) {
                        ToastUtil.s(mContext.getResources().getString(R.string.change_repay_amount_err));
                        return;
                    }
                    int result1 = changeBig.compareTo(oriBig);
                    if (result1 == -1) {
                        ToastUtil.s(mContext.getResources().getString(R.string.change_repay_amount_less_err));
                        return;
                    }
                    Log.d("mCTDeAmountListener", "onClick: " + mChanageAmount);
                    mCTDialogChangeAmountListener.onConfirmClick(mChanageAmount);
                    dismiss();
                }
            }
            dismiss();
        });
    }

    public ZHIDialogChangeAmount setOnClickButtonListener(CTDialogChangeAmountListener onClickButtonListener) {
        this.mCTDialogChangeAmountListener = onClickButtonListener;
        return this;
    }

    public ZHIDialogChangeAmount setOriAmount(String oriAmount) {
        this.mOriAmount = StringUtil.safeString(oriAmount);
        return this;
    }

    public ZHIDialogChangeAmount setDefaultAmount(String defaultAmount) {
        this.mDefaultAmount = StringUtil.safeString(defaultAmount);
        this.mChanageAmount = StringUtil.safeString(defaultAmount);
        return this;
    }

    public interface CTDialogChangeAmountListener {
        void onConfirmClick(String changeAmount);
    }
}
